define(["jquery","paths","layer","api","tools"],function($,path,layer,api,tool){
    layer.config({
        path: path.static + "js/lib/layer/"
    });
    var head = {
        openLogin:function () {
          var  dialog = layer.open({
                type: 1,
                title: null,
                closeBtn:null,
                content: $('#dialogLogin'),
                area: ["443px", "453px"],
                btn: null,
                success:function (index) {
                    $("#closeBt").click(function () {
                        layer.close(dialog);
                    })
                }
            })
        },
        checkLogin:function () {
            return  $("#loginUserName").length === 1;
        }
    };

     if($("#loginUserName").length < 1){
         var cookiemane = tool.cookie("userName");
         var password = tool.cookie("password");
         if(cookiemane){
             layer.load(5);
             api.ajaxPost("/account/login",{
                 name:cookiemane,
                 password:password,
                 type:1
             },function (res) {
                 window.location.reload();
             })
         }
     }

     $("#loginBt").click(function () {
         head.openLogin();
     });

    $("a.verityLogin").click(function () {
        if(!head.checkLogin()) {
            head.openLogin();
            return false;
        }
    });

    $(".ipLoginBt").click(function () {
        var type  =  $(this).attr("login_type");
        if(typeof(type) == 'undefined' || type == 2){
            layer.load(5,"ip登录中");
            $(".layui-layer-loading5").before('<span style="color:#57b854">ip登录中</span>')
            api.ajaxPost("/account/login",{},function (res) {
                layer.msg("登录成功",{icon:1,time:1000},function(){
                    //window.location.reload();
                    window.location.href="/";
                });
            })
        }else{
            head.openLogin();
        }
    });


    $("#userLogin").click(function () {
        var type = $(this).attr("type");
        var name = $("input[name=\"user\"]").val();
        var password  = $("input[name=\"password\"]").val();
        var check = $("#checkboxRemember").is(":checked");
        var ajaxData = {
             type:type,
             name:name,
             password:password
        };
        if(name == ""){
            layer.msg("用户名不能为空",{icon:5,time:1000});
          //$("#prompt").html("用户名不能为空");
          return;
        }
        if(password == ""){
            layer.msg("密码不能为空",{icon:5,time:1000});
            //$("#prompt").html("密码不能为空");
            return;
        }
        
        api.ajaxPost("/account/login",ajaxData,function (res) {
            if(!check){
                tool.cookie("userName",name,{expires:7});
                tool.cookie("password",password,{expires:7});
            }else{
                tool.cookie("userName","");
                tool.cookie("password","");
            }
            //layer.load(5);
            layer.msg("登录成功",{icon:1,time:1000},function(){
                    //window.location.reload();
                    window.location.href="/";
                });
        })
    });



    $("#outLogin").click(function () {
        tool.cookie("userName","");
        tool.cookie("password","");
    });

    $(".se-button").click(function () {
        var value =  $("#s_key").val();
        var key_word = $("#keyword").val();
        console.log(key_word);
        if(key_word){
            if(value == 1) {
                window.location.href = "/search/books?key_word="+ key_word;
            }
            if(value == 2){
                window.location.href = "/search/listen?key_word="+ key_word;
            }
            if(value == 3) {
                window.location.href = "/search/periodical?key_word="+ key_word;
            }
            if(value == 4){
                window.location.href = "/search/digital?key_word="+ key_word;
            }
            if(value == 5) {
                window.location.href = "/search/video?key_word="+ key_word;
            }
        }
    });

    return  head;
});
